package com.sali.BFS;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author sali
 * @date 2025/5/2 20:29
 */
public class LeetCode_297 {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        myxulie(sb, root);
        return sb.toString();
    }

    private void myxulie(StringBuilder sb, TreeNode root) {
        if (root == null) {
            sb.append("none,");
        } else {
            sb.append(root.val).append(",");
            myxulie(sb, root.left);
            myxulie(sb, root.right);
        }
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        String[] strArr = data.split(",");
        List<String> strList = new ArrayList<>(Arrays.asList(strArr));
        return myfanxulie(strList);
    }

    private TreeNode myfanxulie(List<String> strList) {
        if (strList.get(0).equals("none")) {
            strList.remove(0);
            return null;
        }
        TreeNode cur = new TreeNode(Integer.valueOf(strList.get(0)));
        strList.remove(0);
        cur.left = myfanxulie(strList);
        cur.right = myfanxulie(strList);

        return cur;
    }

}
